package net.tngou.iask.action.my;

import java.lang.reflect.GenericArrayType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Department;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Place;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.pojo.Treatment;
import net.tngou.iask.pojo.User;
import net.tngou.iask.service.AccessService;
import net.tngou.iask.service.CureService;
import net.tngou.iask.service.DiseaseService;
import net.tngou.iask.service.SurveyService;
import net.tngou.iask.util.IDUtil;

/**
 * 治疗	
 * @author tngou@tngou.net
 *
 */
public class CureAction extends BaseAction {

	/**
	 * 治疗列表
	 */
	public void list() {
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}
		CureService service = new CureService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		root.put("IDUtil", new IDUtil());
		printFreemarker("my/cure_list.ftl", root);
	}
	/**
	 * 添加治疗
	 */
	public void add() {
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}		
		List<?> manages = service.getList("main="+user.getRole()+" AND role=4", User.class);//管理者
		List<?> doctors = service.getList("main="+user.getRole()+" AND role=5", User.class);//医生
		List<?> nurses = service.getList("main="+user.getRole()+" AND role=6", User.class);//护士
		root.put("manages", manages);
		root.put("doctors", doctors);
		root.put("nurses", nurses);
		DiseaseService service = new DiseaseService();
		List<Map<String, Object>> list = service.getList(user.getMain());
		root.put("list", list);

		printFreemarker("my/cure_add.ftl", root);
	}
	
	/**
	 * 更新治疗
	 */
	public void update() {
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}
		long id= ask.getId();
		if(id==0){run_404();return;}
		Cure cure=new Cure().get(id);
		if(cure==null||cure.getMain()!=user.getMain()){run_404();return;}
		Patient patient = new Patient().get(cure.getPatient());
		if(patient==null){run_404();return;}
		CureService service = new CureService();
		
		List<?> places =  service.getList(Place.class);//身体位置
		List<?> departments = service.getList(Department.class);//科室
		List<?> treatments = service.getList(Treatment.class);//治疗方法段
		root.put("places", places);
		root.put("departments", departments);
		root.put("treatments", treatments);
		
		List<?> manages = service.getList("main="+user.getMain()+" AND role=4", User.class);//管理者
		List<?> doctors = service.getList("main="+user.getMain()+" AND role=5", User.class);//医生
		List<?> nurses = service.getList("main="+user.getMain()+" AND role=6", User.class);//护士
		root.put("manages", manages);
		root.put("doctors", doctors);
		root.put("nurses", nurses);

		
		List<Department> cure_departments = service.getDepartment(cure.getId());
		List<Place> cure_places = service.getPlace(cure.getId());
		List<Treatment> cure_treatments = service.getTreatment(cure.getId());
		List<User> cure_users = service.getUser(cure.getId());
		root.put("cure_departments", cure_departments);
		root.put("cure_places", cure_places);
		root.put("cure_treatments", cure_treatments);
		root.put("cure_users", cure_users);
		
		root.put("patient", patient);
		root.put("cure", cure);
		printFreemarker("my/cure_update.ftl", root);
	}
	
	/**
	 * 治疗详情
	 */
	public void show() {
		if(user.getRole()!=3&&user.getRole()==2&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}
		String[] params = request.getParams();
		int id=0;
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			id=Integer.parseInt(params[0]);			
		}
		if(id==0){run_404();return;}
		Cure cure=new Cure().get(id);
		if(cure==null||cure.getMain()!=user.getMain()){run_404();return;}
		Disease disease = new Disease().get(cure.getDisease());
		Patient patient = new Patient().get(cure.getPatient());
		Survey survey = new Survey().get(cure.getSurvey());
		if(disease==null||patient==null||survey==null){run_404();return;}
		CureService service = new CureService();
		List<Department> departments = service.getDepartment(cure.getId());
		List<User> users = service.getUser(cure.getId());
		List<Place> places = service.getPlace(cure.getId());
		List<Treatment> treatments = service.getTreatment(cure.getId());
		root.put("disease", disease);
		root.put("patient", patient);
		root.put("departments", departments);
		root.put("users", users);
		root.put("places", places);
		root.put("treatments", treatments);
		root.put("cure", cure);
		printFreemarker("my/cure_show.ftl", root);
	}
	
	
	/**
	 * 修改疾病
	 */
	public void diseaseupdate() {
		
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}		
		long id= ask.getId();
		if(id==0){run_404();return;}
		Cure cure=new Cure().get(id);
		if(cure==null||cure.getMain()!=user.getMain()){run_404();return;}
		Patient patient = new Patient().get(cure.getPatient());
		if(patient==null){run_404();return;}
		DiseaseService service = new DiseaseService();
		List<Map<String, Object>> list = service.getList(user.getMain());
		root.put("list", list);
		root.put("cure", cure);
		root.put("patient", patient);
		printFreemarker("my/cure_diseaseupdate.ftl", root);
	}
	
	/**
	 * 更新方案
	 */
	public void surveyupdate() {
		if(user.getRole()!=3&&user.getRole()==4&&user.getRole()==5&&user.getRole()==6)
		{
			run_500();return;
		}		
		long id= ask.getId();
		if(id==0){run_404();return;}
		Cure cure=new Cure().get(id);
		if(cure==null||cure.getMain()!=user.getMain()){run_404();return;}
		Patient patient = new Patient().get(cure.getPatient());
		Survey survey = new Survey().get(cure.getSurvey());
		if(patient==null||survey==null){run_404();return;}
		SurveyService service = new SurveyService();
		List<Survey> list = service.getSurvey(cure.getDisease());
		@SuppressWarnings("unchecked")
		List<Survey> ls = (List<Survey>) service.getList("main="+user.getMain()+" AND type=2", Survey.class);
		if(list!=null)
			list.addAll(ls);
		else
			list=ls;
		if(list!=null){
			list.add(0, survey);
		}
		else{
			list = new ArrayList<>();
			list.add(survey);
		}
		root.put("list", list);
		root.put("cure", cure);
		root.put("patient", patient);
		printFreemarker("my/cure_surveyupdate.ftl", root);
		
	}
	
	/**
	 * 添加访问
	 */
	public void accessadd() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Cure cure = new Cure().get(id);
		if(cure==null){run_404();return;}
		Patient patient = new Patient().get(cure.getPatient());
		Survey survey = new Survey().get(cure.getSurvey());
		if(patient==null||survey==null){run_404();return;}
		SurveyService service = new SurveyService();
		List<Survey> list = service.getSurvey(cure.getDisease());
		@SuppressWarnings("unchecked")
		List<Survey> ls = (List<Survey>) service.getList("main="+user.getMain()+" AND type=2", Survey.class);
		if(list!=null)
			list.addAll(ls);
		else
			list=ls;
		if(list!=null){
			list.add(0, survey);
		}
		else{
			list = new ArrayList<>();
			list.add(survey);
		}
		root.put("list", list);
		root.put("patient", patient);
		root.put("cure", cure);
		printFreemarker("my/cure_accessadd.ftl", root);
		
	}
	
	/**
	 * 治疗随访列表
	 */
	public void access() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Cure cure = new Cure().get(id);
		if(cure==null){run_404();return;}
		Patient patient = new Patient().get(cure.getPatient());
		if(patient==null){run_404();return;}
		AccessService service = new AccessService();
		PageUtil page = service.getList(ask.getPage(), ask.getSize(), cure.getId());
		root.put("cure", cure);
		root.put("patient", patient);
		root.put("page", page);
		printFreemarker("my/cure_access.ftl", root);
	}
}
